package org.ade.monak.anak.service.storage;


import java.util.ArrayList;
import java.util.List;

import org.ade.monak.anak.entity.DataMonitoring;
import org.ade.monak.anak.entity.DateMonitoring;
import org.ade.monak.anak.entity.DayMonitoring;
import org.ade.monak.anak.entity.Lokasi;
import org.ade.monak.anak.entity.Ortu;
import org.ade.monak.anak.util.IDGeneratorAnak;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class DatabaseManagerAnak {

	
	public DatabaseManagerAnak (Context context) { 
		this.context = context;
		mDatabaseHelper = new DatabaseHelper(context);
	}
	  
	
	// get.................................................................
	
	
	// get (laporan).......................................................
	

	//.....................................................................
	
	// get last id.........................................................

	public List<String> getIdLokasis(){
		Cursor cursor = 
				actionQuery("SELECT "+COLUMN_ID+" from "+LOCATION_TABLE_NAME+" order by "+COLUMN_ID_LOCATION+" ASC");
		List<String> ids = getIdsFromCursor(cursor);

		if(cursor!=null && !cursor.isClosed()){
			cursor.close();
			if(getDb().isOpen()){
				getDb().close();
			}
		}
		
		return ids;
	}
	public List<String> getIdMonitorings(){
		Cursor cursor = 
				actionQuery("SELECT "+COLUMN_ID+" from "+MONITORING_TABLE_NAME+" order by "+COLUMN_ID+" ASC");
		List<String> ids = getIdsFromCursor(cursor);
		if(cursor!=null && !cursor.isClosed()){
			cursor.close();
			if(getDb().isOpen()){
				getDb().close();
			}
		}
		
		return ids;
	}

	//.....................................................................
	
	public List<Ortu> getAllOrtu(boolean withDataMonitoring){
		if(!getDb().isDbLockedByOtherThreads()){
			Cursor cursor = actionQuery(ORTU_TABLE_NAME,null,null);
			if(cursor!=null && cursor.getCount()>0){
				List<Ortu> ortus = getAllOrtuFromCursor(cursor, withDataMonitoring);
				if(cursor!=null && !cursor.isClosed()){
					cursor.close();
					if(getDb().isOpen()){
						getDb().close();
					}
				}
				return ortus;
			}
			if(cursor!=null && !cursor.isClosed()){
				cursor.close();
				if(getDb().isOpen()){
					getDb().close();
				}
			}
			return null;
		}else{
			return null;
		}
	
	}
	
	public Lokasi getLastLogLokasi(){
		Cursor cursor = 
				actionQuery("SELECT * from "+LOCATION_TABLE_NAME+" where "+COLUMN_LOG+"='"+1+"' order by "+COLUMN_ID_LOCATION+" DESC limit 1");
		if(cursor!=null && cursor.getCount()>0 && cursor.moveToFirst()){
			Lokasi lokasi = getLokasiFromCursor(cursor);
			if(cursor!=null && !cursor.isClosed()){
				cursor.close();
				if(getDb().isOpen()){
					getDb().close();
				}
			}
			return lokasi;
		}else{
			if(cursor!=null && !cursor.isClosed()){
				cursor.close();
				if(getDb().isOpen()){
					getDb().close();
				}
			}
			return null;
		}
		
		
	}

	public List<Lokasi> getAllLokasi(){
		Cursor cursor = actionQuery(LOCATION_TABLE_NAME,null,null);
		if(cursor!=null && cursor.getCount()>0){
			List<Lokasi> lokasis = getAllLokasiFromCursor(cursor);
			if(cursor!=null && !cursor.isClosed()){
				cursor.close();
				if(getDb().isOpen()){
					getDb().close();
				}
			}
			return lokasis;
		}
		if(cursor!=null && !cursor.isClosed()){
			cursor.close();
			if(getDb().isOpen()){
				getDb().close();
			}
		}
		return null;
	}
	

	public List<DataMonitoring> getAllDataMonitorings
		(boolean withOrtu, boolean withWaktuMonitoring){
		Cursor cursor = actionQuery(MONITORING_TABLE_NAME, null,null);
		if(cursor != null && cursor.getCount()>0){
			List<DataMonitoring> dataMonitorings = 
					getDataMonitoringsFromCursor(cursor, withOrtu, withWaktuMonitoring);
			if(cursor!=null && !cursor.isClosed()){
				cursor.close();
				if(getDb().isOpen()){
					getDb().close();
				}
			}
			return dataMonitorings;
		}else{
			if(cursor!=null && !cursor.isClosed()){
				cursor.close();
				if(getDb().isOpen()){
					getDb().close();
				}
			}
			return null;
		}
		
	}
	
	public Ortu getOrtuByNoHp(String noHp, boolean withDataMonitoring){
		Cursor cursor = actionQuery(ORTU_TABLE_NAME, null, COLUMN_NOHP_ORTU+"='"+noHp+"'");
		if(cursor!=null && cursor.getCount()>0 && cursor.moveToFirst()){
			Ortu ortu = getOrtuFromCursor(cursor, withDataMonitoring);
			if(cursor!=null && !cursor.isClosed()){
				cursor.close();
				if(getDb().isOpen()){
					getDb().close();
				}
			}
			return ortu;
		}else{
			if(cursor!=null && !cursor.isClosed()){
				cursor.close();
				if(getDb().isOpen()){
					getDb().close();
				}
			}
			return null;
		}
	}
	
	public Ortu getOrtuByIdOrtu(String idOrtu, boolean withDataMonitoring){
		Cursor cursor = actionQuery(ORTU_TABLE_NAME, null, COLUMN_ID_ORTU+"='"+idOrtu+"'");
		if(cursor!=null && !cursor.isClosed() && cursor.getCount()>0 && cursor.moveToFirst()){
			Ortu ortu = getOrtuFromCursor(cursor, withDataMonitoring);
			if(cursor!=null && !cursor.isClosed()){
				cursor.close();
				if(getDb().isOpen()){
					getDb().close();
				}
			}
			return ortu;
		}else{
			if(cursor!=null && !cursor.isClosed()){
				cursor.close();
				if(getDb().isOpen()){
					getDb().close();
				}
			}
			return null;
		}
	}
	
	
	public List<DataMonitoring> getDataMonitoringsByIdOrtu(String idOrtu){
		Cursor cursor = actionQuery(MONITORING_TABLE_NAME, null, COLUMN_ORTU_MONITORING+"='"+idOrtu+"'");
		if(cursor != null && !cursor.isClosed() && cursor.getCount()>0){
			List<DataMonitoring> dataMonitorings = 
					getDataMonitoringsFromCursor(cursor, false, true);
			if(cursor!=null && !cursor.isClosed()){
				cursor.close();
				if(getDb().isOpen()){
					getDb().close();
				}
			}
			return dataMonitorings;
		}else{
			if(cursor!=null && !cursor.isClosed()){
				cursor.close();
				if(getDb().isOpen()){
					getDb().close();
				}
			}
			return null;
		}
	}
	
	public DataMonitoring getDataMonitoringByIdMonitoringAndOrtu
		(String idDataMonitoring, String idOrtu, boolean withOrtu, boolean withWaktuMonitoring){
		
		Cursor cursor = actionQuery(MONITORING_TABLE_NAME, null,  COLUMN_ORTU_MONITORING+"='"+idOrtu+"' AND "+COLUMN_ID_MONITORING+"='"+idDataMonitoring+"'");
		if(cursor!=null && cursor.getCount()>0 && cursor.moveToFirst()){
			DataMonitoring dataMonitoring = 
					getDataMonitoringFromCursor(cursor, withOrtu, withWaktuMonitoring);
			if(cursor!=null && !cursor.isClosed()){
				cursor.close();
				if(getDb().isOpen()){
					getDb().close();
				}
			}
			return dataMonitoring;
		}else{
			if(cursor!=null && !cursor.isClosed()){
				cursor.close();
				if(getDb().isOpen()){
					getDb().close();
				}
			}
			return null;
		}
	}
	
	
	
	
	public List<Lokasi> getAllLogLokasi(){
		Cursor cursor = 
				actionQuery(LOCATION_TABLE_NAME, null, COLUMN_LOG+"='"+1+"'");
		if(cursor!=null && cursor.getCount()>0){
			List<Lokasi> lokasis = getAllLokasiFromCursor(cursor);
			if(cursor!=null && !cursor.isClosed()){
				cursor.close();
				if(getDb().isOpen()){
					getDb().close();
				}
			}
			return lokasis;
		}
		if(cursor!=null && !cursor.isClosed()){
			cursor.close();
			if(getDb().isOpen()){
				getDb().close();
			}
		}
		return null;
	}
	
	public Lokasi getLokasiByIdLokasi(String idLokasi){

		if(idLokasi!=null&&!idLokasi.equals("")){

		
			Cursor cursor = 
					actionQuery(LOCATION_TABLE_NAME, null, COLUMN_ID_LOCATION+"='"+idLokasi+"'");

				
			if(cursor!=null && cursor.getCount()>0 && cursor.moveToFirst()){
				Lokasi lokasi = 
						getLokasiFromCursor(cursor);
				if(cursor!=null && !cursor.isClosed()){
					cursor.close();
					if(getDb().isOpen()){
						getDb().close();
					}
				}
				return lokasi;
			}else{
				if(cursor!=null && !cursor.isClosed()){
					cursor.close();
					if(getDb().isOpen()){
						getDb().close();
					}
				}
				return null;
			}
		}
		return null;
	}
	
	public List<DateMonitoring> getTanggalMonitoringsByMonitoring(String idMonitoring, boolean withDataMonitoring){
		Cursor cursor = 
				actionQuery(DATE_MONITORING_TABLE_NAME, null, COLUMN_MONITORING_DATE_MONITORING+"='"+idMonitoring+"'");
		if(cursor!=null && cursor.getCount()>0){
			List<DateMonitoring> dateMonitoring = 
					getTanggalsMonitoringsFromCursor(cursor, withDataMonitoring);
			if(cursor!=null && !cursor.isClosed()){
				cursor.close();
				if(getDb().isOpen()){
					getDb().close();
				}
			}
			return dateMonitoring;
		}else{
			if(cursor!=null && !cursor.isClosed()){
				cursor.close();
				if(getDb().isOpen()){
					getDb().close();
				}
			}
			return null;
		}
	}
	
	public List<DateMonitoring> getAllTanggalMonitorings(boolean withDataMonitoring){
		Cursor cursor = 
				actionQuery(DATE_MONITORING_TABLE_NAME, null, null);
		if(cursor!=null && cursor.getCount()>0){
			List<DateMonitoring> dateMonitorings = getTanggalsMonitoringsFromCursor(cursor, withDataMonitoring);
			if(cursor!=null && !cursor.isClosed()){
				cursor.close();
				if(getDb().isOpen()){
					getDb().close();
				}
			}
			return dateMonitorings;
		}else{
			if(cursor!=null && !cursor.isClosed()){
				cursor.close();
				if(getDb().isOpen()){
					getDb().close();
				}
			}
			return null;
		}
	}
	
	public List<DayMonitoring> getHariMonitoringsByMonitoring(String idMonitoring, boolean withDataMonitoring){
		Cursor cursor = 
				actionQuery(DAY_MONITORING_TABLE_NAME, null, COLUMN_MONITORING_DAY_MONITORING+"='"+idMonitoring+"'");
		if(cursor!=null && cursor.getCount()>0){
			List<DayMonitoring> dayMonitorings = getHarisMonitoringsFromCursor(cursor, withDataMonitoring);
			
			if(cursor!=null && !cursor.isClosed()){
				cursor.close();
				if(getDb().isOpen()){
					getDb().close();
				}
			}
			return dayMonitorings;
		}else{
			if(cursor!=null && !cursor.isClosed()){
				cursor.close();
				if(getDb().isOpen()){
					getDb().close();
				}
			}
			return null;
		}
	}
	
	public List<DayMonitoring> getAllHariMonitorings(boolean withDataMonitoring){
		Cursor cursor = 
				actionQuery(DAY_MONITORING_TABLE_NAME, null, null);
		if(cursor!=null && cursor.getCount()>0){
			List<DayMonitoring> dayMonitorings = getHarisMonitoringsFromCursor(cursor, withDataMonitoring);
			if(cursor!=null && !cursor.isClosed()){
				cursor.close();
				if(getDb().isOpen()){
					getDb().close();
				}
			}
			return dayMonitorings;
		}else{
			if(cursor!=null && !cursor.isClosed()){
				cursor.close();
				if(getDb().isOpen()){
					getDb().close();
				}
			}
			return null;
		}
	}
	
	//...................................................................
	
	// delete............................................................
	
	public void deleteLokasi(Lokasi lokasi){
		getDb().delete
			(LOCATION_TABLE_NAME, COLUMN_ID_LOCATION+"='"+lokasi.getId()+"'", null);
		
		
	}

	
	public void deleteAllLokasi(){
		long result = getDb().delete
			(LOCATION_TABLE_NAME, null, null);
		if(result>0){
			LocationFileManager.clearLocation();
		}
	}
	
	public void deleteDateMonitoringByIdIdDate(String id){

		getDb().delete
			(DATE_MONITORING_TABLE_NAME, 
					"_id='"+id+"'", null);
	}
	
	public void deleteDayMonitoringByIdDay(String id){
		getDb().delete
			(DAY_MONITORING_TABLE_NAME, 
					"_id='"+id+"'", null);
	}
	
	public void deleteDateMonitoringByIdMonitoring(String idMonitoring){

		getDb().delete
			(DATE_MONITORING_TABLE_NAME, 
					COLUMN_MONITORING_DATE_MONITORING+"='"+idMonitoring+"'", null);
	}
	
	public void deleteDayMonitoringByIdMonitoring(String idMonitoring){
		getDb().delete
			(DAY_MONITORING_TABLE_NAME, 
				COLUMN_MONITORING_DAY_MONITORING+"='"+idMonitoring+"'", null);
	}
	
	public void deleteAllDataMonitoringByIdOrtu(String idOrtu){
		List<DataMonitoring> dataMonitorings = getDataMonitoringsByIdOrtu(idOrtu);
		if(dataMonitorings==null){
			return;
		}
		for(DataMonitoring dataMonitoring:dataMonitorings){
			deleteDataMonitoring(dataMonitoring, idOrtu);
		}
	}
	
	public void deleteAllDataMonitoring(){
		List<Ortu> ortus = getAllOrtu(true);
		if(ortus!=null){
			for(Ortu ortu:ortus){
				List<DataMonitoring> dataMonitorings = ortu.getDataMonitorings();;
				if(dataMonitorings!=null){
					for(DataMonitoring dataMonitoring:dataMonitorings){
						deleteDataMonitoring(dataMonitoring, ortu.getIdOrtu());
					}	
				}
				
			}
		}
		
			
	}
	
	
	public void deleteAllOrtu(){
		List<Ortu> ortus = getAllOrtu(false);
		if(ortus!= null){
			for(Ortu ortu:ortus){
				deleteOrtu(ortu);
			}	
		}
		
		
	}
	
	public void deleteOrtu(Ortu ortu){
		if(ortu.getIdOrtu()==null || ortu.getIdOrtu().equals("")){
			ortu = getOrtuByNoHp(ortu.getNoHp(), false);	
		}
		getDb().delete
			(ORTU_TABLE_NAME, 
					COLUMN_ID_ORTU+"='"+ortu.getIdOrtu()+"'", null);
		deleteAllDataMonitoringByIdOrtu(ortu.getIdOrtu());
	}
	
	
	public void deleteDataMonitoring(DataMonitoring dataMonitoring, String idOrtu){
		
		dataMonitoring = getDataMonitoringByIdMonitoringAndOrtu(dataMonitoring.getIdMonitoring(), idOrtu, false, true);
		if(dataMonitoring==null){
			return;
		}
		getDb().delete
			(MONITORING_TABLE_NAME, 
					COLUMN_ID_MONITORING+"='"+dataMonitoring.getIdMonitoring()+"'", null);
		List<DateMonitoring> tanggals = dataMonitoring.getTanggals();
		if(tanggals!=null){
			deleteDateMonitoringByIdMonitoring(dataMonitoring.getIdMonitoring());
		}
		
		List<DayMonitoring> haris = dataMonitoring.getHaris();
		if(haris!=null){
			deleteDayMonitoringByIdMonitoring(dataMonitoring.getIdMonitoring());
		}
		
		Lokasi lokasi = dataMonitoring.getLokasi();
		if(lokasi!=null){
			deleteLokasi(lokasi);
		}
		
	}
	

	
	//...................................................................
	
	// update............................................................
	public void updateAllDateMonitoringsByMonitoring(List<DateMonitoring> tanggals){
		if(tanggals.size()>0){
			deleteDateMonitoringByIdMonitoring(tanggals.get(0).getDataMonitoring().getIdMonitoring());
			addTanggalMonitorings(tanggals);		
		}
	
	}
	
	public void updateAllDayMonitoringsByMonitoring(List<DayMonitoring> haris){
		if(haris.size()>0){
			deleteDayMonitoringByIdMonitoring(haris.get(0).getDataMonitoring().getIdMonitoring());
			addHariMonitorings(haris);
		}
	}

	
	public void updateLokasi(Lokasi lokasi){
		ContentValues cv = new ContentValues();
		cv.put(COLUMN_ID_LOCATION, lokasi.getId());
		cv.put(COLUMN_LATITUDE, lokasi.getlatitude());
		cv.put(COLUMN_LONGITUDE, lokasi.getLongitude());
		if(lokasi.isLog()){
			cv.put(COLUMN_LOG, "1");	
		}else{
			cv.put(COLUMN_LOG, "0");
		}
		if(lokasi.getTime()!=0)cv.put(COLUMN_TIME, lokasi.getTime());
		long result = getDb().update(LOCATION_TABLE_NAME, cv, COLUMN_ID_LOCATION+"='"+lokasi.getId()+"'", null);
		if(result>0){
			Log.d("database manager", "mengupdate lokasi berhasil..");
		}else{
			result = getDb().insert(LOCATION_TABLE_NAME,null, cv);
			if(result>0){
				Log.d("database manager", "lokasi di insertkan..");
			}
		}
	}
	
	public void updateDataMonitoring(DataMonitoring dataMonitoring){
		ContentValues cv = new ContentValues();
		cv.put(COLUMN_KET_MONITORING, dataMonitoring.getKeterangan());
		cv.put(COLUMN_STATUS_MONITORING, dataMonitoring.getStatus());
		cv.put(COLUMN_TOLERANCY_MONITORING, dataMonitoring.getTolerancy());
		cv.put(COLUMN_DATE_MULAI_MONITORING, dataMonitoring.getWaktuMulai());
		cv.put(COLUMN_DATE_SELESAI_MONITORING, dataMonitoring.getWaktuSelesai());
		String idLokasi = dataMonitoring.getLokasi().getId();
		if(idLokasi==null || idLokasi.equals("")){
			IDGeneratorAnak idGenerator = new IDGeneratorAnak(context, this);
			idLokasi = idGenerator.getIdLocation();
			dataMonitoring.getLokasi().setId(idLokasi);
		}
		
		
		if(dataMonitoring.isAktif()){
			cv.put(COLUMN_AKTIF_MONITORING, "1");	
		}else{
			cv.put(COLUMN_AKTIF_MONITORING, "0");
		}
		
		
		
		Lokasi lokasi = getLokasiByIdLokasi(idLokasi);
		if(lokasi==null){
			if(dataMonitoring.getOrtu()!=null){
				DataMonitoring datamonitoringLokasi = getDataMonitoringByIdMonitoringAndOrtu(dataMonitoring.getIdMonitoring(), dataMonitoring.getOrtu().getIdOrtu(), false, false);
				deleteLokasi(datamonitoringLokasi.getLokasi());		
			}
		
		}
		
		cv.put(COLUMN_LOCATION_MONITORING, idLokasi);			
		
		long result = getDb().update(MONITORING_TABLE_NAME, cv, COLUMN_ID_MONITORING+"='"+dataMonitoring.getIdMonitoring()+"'", null);
		
		List<DateMonitoring> tanggals = dataMonitoring.getTanggals();
		if(tanggals!=null){
			updateAllDateMonitoringsByMonitoring(tanggals);
		}
		
		List<DayMonitoring> haris = dataMonitoring.getHaris();
		if(haris!=null){
			updateAllDayMonitoringsByMonitoring(haris);
		}
		
		if(result>0){
			if(dataMonitoring.getLokasi()!=null){
				updateLokasi(dataMonitoring.getLokasi());
			}
		}
	}
	

	//...................................................................
	
	// add...............................................................
	
	

	
	public void addDataMonitorings(List<DataMonitoring> dataMonitorings){
		if(dataMonitorings!=null){
			for(DataMonitoring dataMonitoring:dataMonitorings){
				addDataMonitoring(dataMonitoring);
			}
		}
	}
	  
	public void addDataMonitoring( DataMonitoring dataMonitoring ){
		if(dataMonitoring!=null){
			ContentValues cv = new ContentValues();
			cv.put(COLUMN_ID_MONITORING, dataMonitoring.getIdMonitoring());
			cv.put(COLUMN_DATE_MULAI_MONITORING, dataMonitoring.getWaktuMulai());
			cv.put(COLUMN_DATE_SELESAI_MONITORING, dataMonitoring.getWaktuSelesai());			
			cv.put(COLUMN_STATUS_MONITORING, dataMonitoring.getStatus());
			cv.put(COLUMN_TOLERANCY_MONITORING, dataMonitoring.getTolerancy());
			cv.put(COLUMN_KET_MONITORING, dataMonitoring.getKeterangan());
			
			if(dataMonitoring.isAktif()){
				cv.put(COLUMN_AKTIF_MONITORING, "1");	
			}else{
				cv.put(COLUMN_AKTIF_MONITORING, "0");
			}
			
			Ortu ortu = dataMonitoring.getOrtu();
			if(ortu!=null && ortu.getIdOrtu()!=null && !ortu.getIdOrtu().equals("")){
				cv.put(COLUMN_ORTU_MONITORING, ortu.getIdOrtu());
			}
			
			String idLokasi = dataMonitoring.getLokasi().getId();
			if(idLokasi==null || idLokasi.equals("")){
				IDGeneratorAnak idGenerator = new IDGeneratorAnak(context, this);
				idLokasi = idGenerator.getIdLocation();			
				dataMonitoring.getLokasi().setId(idLokasi);
			}
			cv.put(COLUMN_LOCATION_MONITORING, idLokasi);
		
			long result = getDb().insert(MONITORING_TABLE_NAME, null, cv);	
			if(result>0 && dataMonitoring.getTanggals()!=null){
				addTanggalMonitorings(dataMonitoring.getTanggals());
			}
			if(result>0 && dataMonitoring.getHaris()!=null){
				addHariMonitorings(dataMonitoring.getHaris());
			}
			if(result>0){

				DataMonitoringFileManager.addDataMonitoring(dataMonitoring);
				if(dataMonitoring.getLokasi()!=null){
					addLokasi(dataMonitoring.getLokasi());
				}
			}
			
		}
	}
	
	
	public void addLokasis(List<Lokasi> lokasis){
		if(lokasis!=null){
			for(Lokasi lokasi:lokasis){
				addLokasi(lokasi);
			}
		}
	}
	
	public void setTrackOrtu(Ortu ortu){
		ContentValues cv = new ContentValues();
		if(ortu.isTrack()){
			cv.put(COLUMN_TRACK_ORTU, "1");	
		}else{
			cv.put(COLUMN_TRACK_ORTU, "0");	
		}
		
		getDb().update(ORTU_TABLE_NAME, cv, COLUMN_ID_ORTU+"='"+ortu.getIdOrtu()+"'", null);
		
	}
	
	public void setAktifMonitoring(DataMonitoring dataMonitoring){
		ContentValues cv = new ContentValues();
		if(dataMonitoring.isAktif()){
			cv.put(COLUMN_AKTIF_MONITORING, "1");	
		}else{
			cv.put(COLUMN_AKTIF_MONITORING, "0");	
		}
		
		getDb().update(MONITORING_TABLE_NAME, cv, COLUMN_ID_MONITORING+"='"+dataMonitoring.getIdMonitoring()+"'", null);
		
	}
	
	public void addOrtu(Ortu ortu){
		if(ortu!=null){
			ContentValues cv = new ContentValues();
			
			cv.put(COLUMN_ID_ORTU, ortu.getIdOrtu());
			cv.put(COLUMN_ID_ANAK, ortu.getIdAnak());
			cv.put(COLUMN_NOHP_ORTU, ortu.getNoHp());
			
			if(ortu.isTrack()){
				cv.put(COLUMN_TRACK_ORTU, "1");	
			}else{
				cv.put(COLUMN_TRACK_ORTU, "0");
			}
			
			SQLiteDatabase database = getDb();
			if(database!=null){
				long result = getDb().insert(ORTU_TABLE_NAME, null, cv);
				if(result>0){
				}
			}
			
		}
	}
	
	public void addLokasi(Lokasi lokasi){
		if(lokasi!=null){
			ContentValues cv = new ContentValues();
			if(lokasi.getId()==null||lokasi.getId().equals("")){
				IDGeneratorAnak idGenerator = new IDGeneratorAnak(context, this);
				lokasi.setId(idGenerator.getIdLocation());
			}
			cv.put(COLUMN_ID_LOCATION, lokasi.getId());
		
			cv.put(COLUMN_LATITUDE, lokasi.getlatitude());
			cv.put(COLUMN_LONGITUDE, lokasi.getLongitude());
			cv.put(COLUMN_TIME, lokasi.getTime());
			
			if(lokasi.isLog()){
				cv.put(COLUMN_LOG, "1");	
			}else{
				cv.put(COLUMN_LOG, "0");
			}
			
			long result = getDb().insert(LOCATION_TABLE_NAME, null, cv);
			if(result>0){
				LocationFileManager.addLocation(lokasi);
				Log.d("DatabaseManager", "insert anak successfully with id : "+lokasi.getId());
			}
		}
	}
	
	public void addTanggalMonitorings(List<DateMonitoring> tanggalMonitorings){
		if(tanggalMonitorings!=null){
			for(DateMonitoring tanggalMonitoring:tanggalMonitorings){
				addTanggalMonitoring(tanggalMonitoring);
			}
		}
		
	}
	
	public void addTanggalMonitoring(DateMonitoring tanggalMonitoring){
		if(tanggalMonitoring!=null){
			ContentValues cv = new ContentValues();
			cv.put(COLUMN_DATE_DATE_MONITORING, tanggalMonitoring.getDate());
			cv.put(COLUMN_MONITORING_DATE_MONITORING, tanggalMonitoring.getDataMonitoring().getIdMonitoring());
			getDb().insert(DATE_MONITORING_TABLE_NAME, null, cv);	
		}
	}
	
	public void addHariMonitorings(List<DayMonitoring> hariMonitorings){
		if(hariMonitorings!=null){
			for(DayMonitoring hariMonitoring:hariMonitorings){
				addHariMonitoring(hariMonitoring);
			}
		}
		
	}
	
	public void addHariMonitoring(DayMonitoring dayMonitoring){
		if(dayMonitoring!=null){
			ContentValues cv = new ContentValues();
			cv.put(COLUMN_DAY_DAY_MONITORING, dayMonitoring.getHari());
			cv.put(COLUMN_MONITORING_DAY_MONITORING, dayMonitoring.getDataMonitoring().getIdMonitoring());
			
			getDb().insert(DAY_MONITORING_TABLE_NAME, null, cv);	
		}
	}
	
	
	
	//............................................................................
	
	//get from Cursor.............................................................
	
	private List<String> getIdsFromCursor(Cursor cursor){
		List<String> ids = new ArrayList<String>();
		if(cursor.moveToFirst()){
			do{
				ids.add(cursor.getString(cursor.getColumnIndex(COLUMN_ID)));
			}while(cursor.moveToNext());
			cursor.close();
		}
		return ids;
	}

	private List<Lokasi> getAllLokasiFromCursor(Cursor cursor){
		List<Lokasi> lokasis = new ArrayList<Lokasi>();
		if(cursor.moveToFirst()){
			do{
				lokasis.add(getLokasiFromCursor(cursor));
			}while(cursor.moveToNext());
		}
		return lokasis;
	}
	
	private List<Ortu> getAllOrtuFromCursor(Cursor cursor, boolean withDataMonitoring){
		List<Ortu> ortus = new ArrayList<Ortu>();
		if(cursor.moveToFirst()){
			do{
				ortus.add(getOrtuFromCursor(cursor, withDataMonitoring));
			}while(cursor.moveToNext());
		}
		return ortus;
	}
	
	private Ortu getOrtuFromCursor(Cursor cursor, boolean withDataMonitoring){
		
		if(cursor!=null && cursor.getCount()>0){
			
			Ortu ortu = new Ortu();
			 
			int indexIdOrtu = cursor.getColumnIndex(COLUMN_ID_ORTU);
			int indexIdAnak	= cursor.getColumnIndex(COLUMN_ID_ANAK);
			int indexTrack	= cursor.getColumnIndex(COLUMN_TRACK_ORTU);
			int indexNoHp	= cursor.getColumnIndex(COLUMN_NOHP_ORTU);
			
			ortu.setIdOrtu(cursor.getString(indexIdOrtu));
			ortu.setIdAnak(cursor.getString(indexIdAnak));
			ortu.setNoHp(cursor.getString(indexNoHp));
						
			if(cursor.getString(indexTrack).equals("1")){
				ortu.setTrack(true);
			}else{
				ortu.setTrack(false);
			}
			
			if(withDataMonitoring){
				ortu.setDataMonitorings(getDataMonitoringsByIdOrtu(ortu.getIdOrtu()));
			}
			
			return ortu;
		}
		return null;
	}
	
	private List<DataMonitoring> getDataMonitoringsFromCursor
		(Cursor cursor, boolean withOrtu, boolean withWaktuMonitoring){
		List <DataMonitoring> dataMonitorings = new ArrayList<DataMonitoring>();
		if(cursor.moveToFirst()){
			do{
				 dataMonitorings.add
				 	(getDataMonitoringFromCursor(cursor, withOrtu,withWaktuMonitoring));
			}while(cursor.moveToNext());
		}
		return dataMonitorings;
	}
	
	
	
	private DataMonitoring getDataMonitoringFromCursor
		(Cursor cursor, boolean withOrtu, boolean withWaktuMonitoring){
		if(cursor!=null && cursor.getCount()>0){
			
			DataMonitoring dataMonitoring = new DataMonitoring();
			 
			int indexIdMonitoring 			= cursor.getColumnIndex(COLUMN_ID_MONITORING);
			int indexKetMonitoring			= cursor.getColumnIndex(COLUMN_KET_MONITORING);
			int indexLocationMonitoring 	= cursor.getColumnIndex(COLUMN_LOCATION_MONITORING);
			int indexDateMulaiMonitoring	= cursor.getColumnIndex(COLUMN_DATE_MULAI_MONITORING);
			int indexDateSelesaiMonitoring	= cursor.getColumnIndex(COLUMN_DATE_SELESAI_MONITORING);
			int indexStatusMonitoring 		= cursor.getColumnIndex(COLUMN_STATUS_MONITORING);
			int indexTolerancy				= cursor.getColumnIndex(COLUMN_TOLERANCY_MONITORING);
			int indexOrtu					= cursor.getColumnIndex(COLUMN_ORTU_MONITORING);
			int indexAktif 					= cursor.getColumnIndex(COLUMN_AKTIF_MONITORING);	
			
			dataMonitoring.setIdMonitoring(cursor.getString(indexIdMonitoring));
			dataMonitoring.setOrtu(getOrtuByIdOrtu(cursor.getString(indexOrtu), false));
			dataMonitoring.setLokasi(getLokasiByIdLokasi(cursor.getString(indexLocationMonitoring)));
			dataMonitoring.setKeterangan(cursor.getString(indexKetMonitoring));
			dataMonitoring.setWaktuMulai(cursor.getLong(indexDateMulaiMonitoring));
			dataMonitoring.setWaktuSelesai(cursor.getLong(indexDateSelesaiMonitoring));
			dataMonitoring.setStatus(cursor.getInt(indexStatusMonitoring));
			dataMonitoring.setTolerancy(cursor.getInt(indexTolerancy));
			if(cursor.getString(indexAktif).equals("1")){
				dataMonitoring.setAktif(true);
			}else{
				dataMonitoring.setAktif(false);
			}
			
			if(withWaktuMonitoring){
				dataMonitoring.setTanggals
					(getTanggalMonitoringsByMonitoring
							(dataMonitoring.getIdMonitoring(), false));
				
				dataMonitoring.setHaris
					(getHariMonitoringsByMonitoring
							(dataMonitoring.getIdMonitoring(), false));
			}
			
			return dataMonitoring;
		}
		return null;
		
	}
	
	
	

	
	public List<DateMonitoring> getTanggalsMonitoringsFromCursor(Cursor cursor, boolean withDataMonitoring){
		List<DateMonitoring> waktuMonitorings = new ArrayList<DateMonitoring>();
		if(cursor.moveToFirst()){
			do{
				 waktuMonitorings.add(getTanggalMonitoringFromCursor(cursor, withDataMonitoring));
			}while(cursor.moveToNext());
		}
		return waktuMonitorings;
	}
	
	public List<DayMonitoring> getHarisMonitoringsFromCursor(Cursor cursor, boolean withDataMonitoring){
		List<DayMonitoring> hariMonitorings = new ArrayList<DayMonitoring>();
		if(cursor.moveToFirst()){
			do{
				 hariMonitorings.add(getHariMonitoringFromCursor(cursor, withDataMonitoring));
			}while(cursor.moveToNext());
		}
		return hariMonitorings;
	}
	
	public Lokasi getLokasiFromCursor(Cursor cursor){
		if(cursor!=null && cursor.getCount()>0){
			Lokasi lokasi = new Lokasi();
			int indexIdLokasi	= cursor.getColumnIndex(COLUMN_ID_LOCATION);
			int indexLatitude 	= cursor.getColumnIndex(COLUMN_LATITUDE);
			int indexLongitude 	= cursor.getColumnIndex(COLUMN_LONGITUDE);
			int indexLog		= cursor.getColumnIndex(COLUMN_LOG);
			int indexTime		= cursor.getColumnIndex(COLUMN_TIME);
			
			lokasi.setId(cursor.getString(indexIdLokasi));
			if(cursor.getString(indexLog).equals("1")){
				lokasi.setLog(true);
			}else{
				lokasi.setLog(false);
			}
			lokasi.setLatitude(cursor.getDouble(indexLatitude));
			lokasi.setLongitude(cursor.getDouble(indexLongitude));
			lokasi.setTime(cursor.getLong(indexTime));
			
			return lokasi;
		}
		return null;
	}
	
	public DateMonitoring getTanggalMonitoringFromCursor(Cursor cursor, boolean withDataMonitoring){
		DateMonitoring dateMonitoring = new DateMonitoring();
		if(cursor!=null && cursor.getCount()>0){
			int indexId		= cursor.getColumnIndex("_id");
			int indexDate 	= cursor.getColumnIndex(COLUMN_DATE_DATE_MONITORING);
			
			String idDateMonitoring = cursor.getInt(indexId)+"";
			long tanggalMonitoring = cursor.getLong(indexDate);
			dateMonitoring.setId(idDateMonitoring);
			dateMonitoring.setDate(tanggalMonitoring);
			
			if(withDataMonitoring){
//				int indexMonitoring = cursor.getColumnIndex(COLUMN_MONITORING_DATE_MONITORING);
//				String idMonitoring = cursor.getString(indexMonitoring);
//				dateMonitoring.setDataMonitoring(getDataMonitoringByIdMonitoring(idMonitoring,false, false));
			}
			
			return dateMonitoring;
		}
		return null;
	}
	public DayMonitoring getHariMonitoringFromCursor(Cursor cursor, boolean withDataMonitoring){
		DayMonitoring dayMonitoring = new DayMonitoring();
		if(cursor!=null && cursor.getCount()>0){
			int indexDay 		= cursor.getColumnIndex(COLUMN_DAY_DAY_MONITORING);
			int indexId			= cursor.getColumnIndex("_id");
			String idHari		= cursor.getInt(indexId)+"";
			int hariMonitoring 	= cursor.getInt(indexDay);
			dayMonitoring.setId(idHari);
			dayMonitoring.setHari(hariMonitoring);
			if(withDataMonitoring){
//				int indexMonitoring = cursor.getColumnIndex(COLUMN_MONITORING_DAY_MONITORING);
//				String idMonitoring = cursor.getString(indexMonitoring);
//				dayMonitoring.setDataMonitoring(getDataMonitoringByIdMonitoring(idMonitoring,false, false));
			}
			return dayMonitoring;
		}
		return dayMonitoring;
	}
	//............................................................................
	//method buat melakukan query.................................................
	private Cursor actionQuery(String table, String[]columns, String selection){
	  
		SQLiteDatabase db 	= getDb();
		
		if(!db.isDbLockedByOtherThreads() && db.isOpen()){
			final Cursor cursor = 
					db.query(table, columns, selection, null, null, null, null, null);
			db = null;
			return cursor;	
		}else{
			return null;
		}
		
	}
	
	private Cursor actionQuery(String query){
		SQLiteDatabase db 	= getDb();
		final Cursor cursor = 
				db.rawQuery(query, null);
		db = null;
		return cursor;
	}
	  
	private static SQLiteDatabase getDb(){
		if(mDatabaseHelper.getWritableDatabase().isOpen()){
			mDatabaseHelper.getWritableDatabase().close();
		}
		return mDatabaseHelper.getWritableDatabase();
	}
	  
	private class DatabaseHelper extends SQLiteOpenHelper{
		
	    
	    public DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}
	
	    private void createTables(SQLiteDatabase db){
	    	db.execSQL(CREATE_LOCATION);
			db.execSQL(CREATE_MONITORING);
			db.execSQL(CREATE_ORTU);
			db.execSQL(CREATE_DATE_MONITORING);
			db.execSQL(CREATE_DAY_MONITORING);
	    }
	    
	    private void dropTables(SQLiteDatabase db){
	    	db.execSQL("DROP TABLE IF EXISTS " + LOCATION_TABLE_NAME);
			db.execSQL("DROP TABLE IF EXISTS " + MONITORING_TABLE_NAME);
			db.execSQL("DROP TABLE IF EXISTS " + ORTU_TABLE_NAME);
			db.execSQL("DROP TABLE IF EXISTS " + DATE_MONITORING_TABLE_NAME);
			db.execSQL("DROP TABLE IF EXISTS " + DAY_MONITORING_TABLE_NAME);
			
	    }
	    
		@Override
		public void onCreate(SQLiteDatabase db) {
			createTables(db);
		}
	
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			dropTables(db);
			createTables(db);
		}
		
		private static final String DATABASE_NAME = "monak_anak.db";
	    private static final int DATABASE_VERSION = 8;
		
	    private static final String CREATE_ORTU =
	    		"CREATE TABLE IF NOT EXISTS "+
	    	    ORTU_TABLE_NAME+" ("+
	    		COLUMN_ID_ORTU+" VARCHAR(30) PRIMARY KEY,"+
	    	    COLUMN_ID_ANAK+" VARCHAR(10),"+
	    	    COLUMN_NOHP_ORTU+" VARCHAR(20),"+
	    		COLUMN_TRACK_ORTU+" VARCHAR(1))";
	    
	    private static final String CREATE_MONITORING = 
	    		"CREATE TABLE IF NOT EXISTS "+
	    		MONITORING_TABLE_NAME+" ("+
	    		"_id INTEGER PRIMARY KEY,"+
	    		COLUMN_ID_MONITORING+" VARCHAR(10),"+
	    		COLUMN_ORTU_MONITORING+" VARCHAR(30),"+
	    		COLUMN_KET_MONITORING+" VARCHAR(50),"+
	    		COLUMN_LOCATION_MONITORING+" VARCHAR(10),"+
	    		COLUMN_DATE_MULAI_MONITORING+" INTEGER,"+	    		
	    		COLUMN_DATE_SELESAI_MONITORING+" INTEGER,"+
	    		COLUMN_STATUS_MONITORING+" INTEGER,"+	    
	    		COLUMN_AKTIF_MONITORING+" VARCHAR(1),"+		
	    		COLUMN_TOLERANCY_MONITORING+" INTEGER)";
	    
	    private static final String CREATE_LOCATION = 
	    		"CREATE TABLE IF NOT EXISTS "+
	    		LOCATION_TABLE_NAME+" ("+
	    		COLUMN_ID_LOCATION+" VARCHAR(15) PRIMARY KEY,"+	   
	    		COLUMN_LONGITUDE+" REAL,"+
	    		COLUMN_LATITUDE+" REAL,"+
	    		COLUMN_LOG+" VARCHAR(5),"+
	    		COLUMN_TIME+" INTEGER)";
	    
	    
	    private static final String CREATE_DATE_MONITORING = 
	    		"CREATE TABLE IF NOT EXISTS "+
	    		DATE_MONITORING_TABLE_NAME+" ("+
	    		"_id INTEGER PRIMARY KEY,"+
	    		COLUMN_DATE_DATE_MONITORING+" INTEGER,"+	    	
	    		COLUMN_MONITORING_DATE_MONITORING+ " VARCHAR(10),"+			
	    		"FOREIGN KEY("+COLUMN_MONITORING_DATE_MONITORING+") REFERENCES "+
	    		MONITORING_TABLE_NAME+"("+COLUMN_ID_MONITORING+"))";
	    
	    private static final String CREATE_DAY_MONITORING = 
	    		"CREATE TABLE IF NOT EXISTS "+
	    		DAY_MONITORING_TABLE_NAME+" ("+	
	    		"_id INTEGER PRIMARY KEY,"+
	    		COLUMN_DAY_DAY_MONITORING+" INTEGER,"+	    	
	    		COLUMN_MONITORING_DAY_MONITORING+ " VARCHAR(10),"+
	    		"FOREIGN KEY("+COLUMN_MONITORING_DATE_MONITORING+") REFERENCES "+
	    		MONITORING_TABLE_NAME+"("+COLUMN_ID_MONITORING+"))";
	    
	}
	
	private final Context context;
	
	private static DatabaseHelper mDatabaseHelper = null;
	
	    
	
	private static final String COLUMN_ID 		= "id";
	    
    private static final String MONITORING_TABLE_NAME			= 
    		"monitoring";
    private static final String COLUMN_ID_MONITORING			= "id";
    private static final String COLUMN_ORTU_MONITORING			= "ortu";
    private static final String COLUMN_KET_MONITORING			= "ket";
    private static final String COLUMN_LOCATION_MONITORING		= "lokasi";
    private static final String COLUMN_DATE_MULAI_MONITORING	= "mulai";
    private static final String COLUMN_DATE_SELESAI_MONITORING	= "selesai";
    private static final String COLUMN_STATUS_MONITORING		= "status";
    private static final String COLUMN_AKTIF_MONITORING			= "aktif";
    private static final String COLUMN_TOLERANCY_MONITORING		= "tolerancy";
    
    private static final String ORTU_TABLE_NAME					=
    		"ortu";
    private static final String COLUMN_ID_ORTU					= "id";
    private static final String COLUMN_ID_ANAK					= "anak";
    private static final String COLUMN_NOHP_ORTU				= "noHp";
    private static final String COLUMN_TRACK_ORTU				= "track";

    private static final String DAY_MONITORING_TABLE_NAME		=
    		"day";
    private static final String COLUMN_MONITORING_DAY_MONITORING	= "monitoring";
    private static final String COLUMN_DAY_DAY_MONITORING			= "day";
    
    private static final String LOCATION_TABLE_NAME				=
    		"lokasi";
    private static final String COLUMN_ID_LOCATION	= "id";
    private static final String COLUMN_LONGITUDE 	= "longitude";
    private static final String COLUMN_LATITUDE		= "latitude";
    private static final String COLUMN_TIME			= "time";
    private static final String COLUMN_LOG			= "log";
    
    private static final String DATE_MONITORING_TABLE_NAME			=
    		"date";
    private static final String COLUMN_MONITORING_DATE_MONITORING	= "monitoring";
    private static final String COLUMN_DATE_DATE_MONITORING			= "date";
      
}
